package com.pro01.backend.mapper;

import com.pro01.backend.pojo.ForumPost;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ForumPostMapper {

    // 发布帖子
    @Insert("""
        INSERT INTO forum_posts (forum_id, user_id, title, content, is_pinned, status, view_count, comment_count, created_at, updated_at)
        VALUES (#{forumId}, #{userId}, #{title}, #{content}, #{isPinned}, #{status}, #{viewCount}, #{commentCount}, #{createdAt}, #{updatedAt})
    """)
    int insertPost(ForumPost post);

    // 用户查询所有帖子
    // 查询板块下所有的帖子
//    @Select("""
//    SELECT * FROM forum_posts
//    WHERE forum_id = #{forumId}
//      AND status = 'ACTIVE'
//    ORDER BY is_pinned DESC, created_at DESC
//    LIMIT #{offset}, #{pageSize}
//""")
//    List<ForumPost> selectPostsByForum(@Param("forumId") Long forumId,
//                                       @Param("offset") int offset,
//                                       @Param("pageSize") int pageSize);
    @Select("""
    SELECT p.*, u.username, u.title AS userTitle,
           u.nickname_color AS nicknameColor, u.avatar_frame AS avatarFrame
    FROM forum_posts p
    LEFT JOIN users u ON p.user_id = u.id
    WHERE p.forum_id = #{forumId}
      AND p.status = 'ACTIVE'
    ORDER BY p.is_pinned DESC, p.created_at DESC
    LIMIT #{offset}, #{pageSize}
""")
    List<ForumPost> selectPostsByForum(@Param("forumId") Long forumId,
                                         @Param("offset") int offset,
                                         @Param("pageSize") int pageSize);

    @Select("""
    SELECT COUNT(*) FROM forum_posts
    WHERE forum_id = #{forumId}
      AND status = 'ACTIVE'
""")
    int countPostsByForum(@Param("forumId") Long forumId);

    // 查看帖子详情
//    @Select("""
//    SELECT * FROM forum_posts
//    WHERE id = #{postId}
//      AND status = 'ACTIVE'
//""")
//    ForumPost getPostById(Long postId);
    @Select("""
    SELECT p.*, u.username, u.title AS userTitle,
           u.nickname_color AS nicknameColor, u.avatar_frame AS avatarFrame
    FROM forum_posts p
    LEFT JOIN users u ON p.user_id = u.id
    WHERE p.id = #{postId}
      AND p.status = 'ACTIVE'
""")
    ForumPost getPostById(@Param("postId") Long postId);


    @Update("UPDATE forum_posts SET view_count = view_count + 1 WHERE id = #{postId}")
    int increaseViewCount(Long postId);

    // 编辑帖子
    @Update("""
    UPDATE forum_posts
    SET title = #{title},
        content = #{content},
        updated_at = NOW()
    WHERE id = #{id}
""")
    int updatePost(ForumPost post);

    // 用户删除帖子
    @Update("""
    UPDATE forum_posts
    SET status = 'DELETED'
    WHERE id = #{postId}
""")
    int softDeletePost(Long postId);

//    // 模糊查询帖子
//    @Select("""
//    SELECT * FROM forum_posts
//    WHERE title LIKE CONCAT('%', #{title}, '%')
//      AND status = 'ACTIVE'
//    ORDER BY is_pinned DESC, created_at DESC
//    LIMIT #{offset}, #{pageSize}
//""")
//    List<ForumPost> searchPostsByTitle(@Param("title") String title,
//                                       @Param("offset") int offset,
//                                       @Param("pageSize") int pageSize);

    @Select("""
    SELECT p.*, u.username, u.title AS userTitle,
           u.nickname_color AS nicknameColor, u.avatar_frame AS avatarFrame
    FROM forum_posts p
    LEFT JOIN users u ON p.user_id = u.id
    WHERE p.title LIKE CONCAT('%', #{title}, '%') 
      AND p.status = 'ACTIVE'
    ORDER BY p.is_pinned DESC, p.created_at DESC
    LIMIT #{offset}, #{pageSize}
""")
    List<ForumPost> searchPostsByTitle(@Param("title") String title,
                                       @Param("offset") int offset,
                                       @Param("pageSize") int pageSize);

    @Select("""
    SELECT COUNT(*) FROM forum_posts
    WHERE title LIKE CONCAT('%', #{title}, '%') 
      AND status = 'ACTIVE'
""")
    int countPostsByTitle(@Param("title") String title);

    /**
     * 接下来是管理员的接口
     */

    // 查询当前板块下的所有帖子(仅限管理员)
    @Select("""
    SELECT * FROM forum_posts
    WHERE forum_id = #{forumId}
      AND status IN ('ACTIVE', 'HIDDEN', 'DELETED')
    ORDER BY is_pinned DESC, created_at DESC
    LIMIT #{offset}, #{pageSize}
""")
    List<ForumPost> selectPostsByForumForAdmin(@Param("forumId") Long forumId,
                                               @Param("offset") int offset,
                                               @Param("pageSize") int pageSize);

    @Select("""
    SELECT COUNT(*) FROM forum_posts
    WHERE forum_id = #{forumId}
      AND status IN ('ACTIVE', 'HIDDEN', 'DELETED')
""")
    int countPostsByForumForAdmin(@Param("forumId") Long forumId);

    // 删除帖子(仅限管理员)
    @Update("""
    UPDATE forum_posts
    SET status = 'DELETED', updated_at = NOW()
    WHERE id = #{postId} AND forum_id = #{forumId}
""")
    int deletePostByAdmin(@Param("postId") Long postId, @Param("forumId") Long forumId);

    // 恢复帖子(仅限管理员)
    @Update("""
    UPDATE forum_posts
    SET status = 'ACTIVE', updated_at = NOW()
    WHERE id = #{postId} AND forum_id = #{forumId}
      AND status IN ('DELETED', 'HIDDEN')
""")
    int restorePost(@Param("postId") Long postId, @Param("forumId") Long forumId);

    @Select("""
    SELECT * FROM forum_posts
    WHERE id = #{postId}
""")
    ForumPost getAllPostById(Long postId);

    // 隐藏帖子(仅限管理员)
    @Update("""
    UPDATE forum_posts
    SET status = 'HIDDEN', updated_at = NOW()
    WHERE id = #{postId} AND forum_id = #{forumId}
      AND status = 'ACTIVE'
""")
    int hidePost(@Param("postId") Long postId, @Param("forumId") Long forumId);

    // 用户查看自己发的所有帖子
    @Select("""
    SELECT p.*, f.name AS forumName
    FROM forum_posts p
    LEFT JOIN forums f ON p.forum_id = f.id
    WHERE p.user_id = #{userId}
      AND p.status = 'ACTIVE'
    ORDER BY p.created_at DESC
    LIMIT #{offset}, #{pageSize}
""")
    List<ForumPost> selectPostsByUser(@Param("userId") Long userId,
                                      @Param("offset") int offset,
                                      @Param("pageSize") int pageSize);

    @Select("""
    SELECT COUNT(*)
    FROM forum_posts
    WHERE user_id = #{userId}
      AND status = 'ACTIVE'
""")
    int countPostsByUser(@Param("userId") Long userId);

    // 用户隐藏自己的帖子
    @Update("""
    UPDATE forum_posts
    SET status = 'HIDDEN', updated_at = NOW()
    WHERE id = #{postId}
      AND user_id = #{userId}
      AND status = 'ACTIVE'
""")
    int hidePostByUser(@Param("postId") Long postId, @Param("userId") Long userId);

    // 用户取消隐藏自己的帖子
    @Update("""
    UPDATE forum_posts
    SET status = 'ACTIVE', updated_at = NOW()
    WHERE id = #{postId}
      AND user_id = #{userId}
      AND status = 'HIDDEN'
""")
    int restorePostByUser(@Param("postId") Long postId, @Param("userId") Long userId);

    @Select("""
    SELECT * FROM forum_posts
    WHERE id = #{postId}
      AND status = 'HIDDEN'
""")
    ForumPost getHiddenPostById(Long postId);

    // 置顶帖子(仅限管理员)
    @Update("""
    UPDATE forum_posts
    SET is_pinned = true, updated_at = NOW()
    WHERE id = #{postId}
      AND forum_id = #{forumId}
""")
    int pinPost(@Param("postId") Long postId, @Param("forumId") Long forumId);

    // 取消置顶帖子(仅限管理员)
    @Update("""
    UPDATE forum_posts
    SET is_pinned = false, updated_at = NOW()
    WHERE id = #{postId}
      AND forum_id = #{forumId}
""")
    int unpinPost(@Param("postId") Long postId, @Param("forumId") Long forumId);

    // 点赞数 +1
    @Update("""
    UPDATE forum_posts
    SET like_count = like_count + 1
    WHERE id = #{postId}
""")
    int incrementLikeCount(@Param("postId") Long postId);

    // 点赞数 -1
    @Update("""
    UPDATE forum_posts
    SET like_count = like_count - 1
    WHERE id = #{postId} AND like_count > 0
""")
    int decrementLikeCount(@Param("postId") Long postId);

    // 根据帖子查找用户，点赞加积分用
    @Select("SELECT user_id FROM forum_posts WHERE id = #{postId}")
    Long getPostOwnerId(@Param("postId") Long postId);

}
